package com.dongliu.controller;

import com.dongliu.domain.Admin;
import com.dongliu.domain.Notice;
import com.dongliu.domain.User;
import com.dongliu.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @PutMapping
    public Result update(@RequestBody Notice notice) {
        boolean flag = noticeService.update(notice);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR, flag);
    }

    @GetMapping
    public Result getContent(HttpServletRequest request) {
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        User user = (User) request.getSession().getAttribute("user");
        if(admin == null && user == null)
            return new Result(Code.LOGIN_ERR, null, "请登录后再执行操作！");
        Notice notice = noticeService.getContent();
        Integer code = notice != null ? Code.GET_OK : Code.GET_ERR;
        String msg = notice != null ? "":"数据查询失败，请重试！";
        return new Result(code, notice, msg);
    }

}
